package com.wx.mall.sys.controller;


import com.wx.mall.common.ResultVo;
import com.wx.mall.common.utils.WebTools;
import com.wx.mall.sys.entity.BasicSetting;
import com.wx.mall.sys.service.IBasicSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author j
 * @since 2020-03-17
 */
@RestController
@RequestMapping("/basic-setting")
@Api(tags = "基本设置接口")
@Slf4j
public class BasicSettingController {
    @Value("${localhost.file.path}")
    private String filePath;
    @Autowired
    private IBasicSettingService iBasicSettingService;

    @ApiOperation("查询基本设置")
    @GetMapping("/get")
    public String get(){
        return new ResultVo(ResultVo.ErrorCode.SUCCESS,"",iBasicSettingService.get()).toString();
    }

    @ApiOperation("保存基本设置")
    @PostMapping("/save")
    public String save(@RequestParam(value = "file",required = false) @ApiParam("商品默认图片") MultipartFile file,
                       @ApiParam("热门关键字") String hotKeyword,
                       @ApiParam("时间") String webDate,
                       @ApiParam("货币") String currency,
                       @ApiParam("退货上传图片凭证") String imgNum,
                       @ApiParam("商品评价是否审核") String isAudit,
                       @ApiParam("站点统计代码") String statistical,
                       @ApiParam("最小购物金额") String minMoney,
                       @ApiParam("是否开启自动确认收货") String isAutoGoods,
                       @ApiParam("能否开发票") String isInvoice,
                       @ApiParam("发票内容选项") String invoiceContent,
                       @ApiParam("减库存的时机") String reductionInventory,
                       @ApiParam("增加销量的时机") String addSales,
                       @ApiParam("修改订单发票信息时") String updateInvoice,
                       @ApiParam("修改订单发货人信息时") String updateGood,
                       @ApiParam("修改订单商品信息时") String updateGoodInfo,
                       @ApiParam("修改订单费用信息时") String updateCost,
                       @ApiParam("关闭订单时") String closeOrder,
                       @ApiParam("取消订单时") String cancelOrder,
                       @ApiParam("退货处理时") String returnGoods,
                       @ApiParam("退货确认收货时") String returnGoodsConfirm,
                       @ApiParam("退款处理时") String returnMoney,
                       @ApiParam("修改匠币或成长值时") String upddateCurrency,
                       @ApiParam("用户协议") String userAgreement) throws IOException {
        BasicSetting basicSetting = new BasicSetting();
        basicSetting.setHotKeyword(hotKeyword);
        basicSetting.setWebDate(new Date(webDate));
        basicSetting.setCurrency(currency);
        basicSetting.setImgNum(imgNum);
        basicSetting.setIsAudit(isAudit);
        basicSetting.setStatistical(statistical);
        basicSetting.setMinMoney(minMoney);
        basicSetting.setIsAudit(isAudit);
        basicSetting.setIsAutoGoods(isAutoGoods);
        basicSetting.setIsInvoice(isInvoice);
        basicSetting.setInvoiceContent(invoiceContent);
        basicSetting.setReductionInventory(reductionInventory);
        basicSetting.setAddSales(addSales);
        basicSetting.setUpdateInvoice(updateInvoice);
        basicSetting.setUpdateGood(updateGood);
        basicSetting.setUpdateGoodInfo(updateGoodInfo);
        basicSetting.setUpdateCost(updateCost);
        basicSetting.setCloseOrder(closeOrder);
        basicSetting.setCancelOrder(cancelOrder);
        basicSetting.setReturnGoods(returnGoods);
        basicSetting.setReturnGoodsConfirm(returnGoodsConfirm);
        basicSetting.setReturnMoney(returnMoney);
        basicSetting.setUpddateCurrency(upddateCurrency);
        basicSetting.setUserAgreement(userAgreement);
        String imgId = WebTools.getRandomID()+".png";
        if(file != null){
            WebTools.cacheFile(file,filePath,imgId);
            basicSetting.setGoodDefaultImg("/showImg?id="+imgId+"&token=0");
        }else{
            basicSetting.setGoodDefaultImg(iBasicSettingService.get().getGoodDefaultImg());
        }
        iBasicSettingService.save2(basicSetting);
        return new ResultVo(ResultVo.ErrorCode.SUCCESS,"提交成功！","").toString();
    }
}

